package com.zhufeng.materialdesign.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhufeng.materialdesign.bean.AnimalBean;
import com.zhufeng.materialdesign.R;
import com.zhufeng.materialdesign.ui.AnimalActivity;

import java.util.List;

/**
 * Created by 18810 on 2017/3/7.
 */

public class AnimalAdapter extends RecyclerView.Adapter<AnimalAdapter.ViewHolder>{

    private Context mContext;
    private List<AnimalBean> list;

    public AnimalAdapter(List<AnimalBean> list) {
        this.list = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(mContext==null){
            mContext=parent.getContext();
        }
        View view= LayoutInflater.from(mContext).inflate(R.layout.animal_item,
                parent,false);
        //设置条目点击事件，跳转界面
        final ViewHolder holder = new ViewHolder(view);
        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position=holder.getAdapterPosition();
                AnimalBean animalBean=list.get(position);
                Intent intent=new Intent(mContext, AnimalActivity.class);
                intent.putExtra(AnimalActivity.ANIMAL_NAME,animalBean.getName());
                intent.putExtra(AnimalActivity.ANIMAL_IMAGE_ID,animalBean.getImageId());
                mContext.startActivity(intent);
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        AnimalBean animalBean=list.get(position);
        holder.animal_name.setText(animalBean.getName());
        //Glide设置图片资源
        Glide.with(mContext).load(animalBean.getImageId()).into(holder.animal_image);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        CardView cardView;
        ImageView animal_image;
        TextView animal_name;

        public ViewHolder(View itemView) {
            super(itemView);
            cardView= (CardView) itemView;
            animal_image= (ImageView) itemView.findViewById(R.id.animal_image);
            animal_name= (TextView) itemView.findViewById(R.id.animal_name);
        }
    }
}
